﻿using System;
using System.Windows.Forms;
using PluginInterface;
using System.Drawing;
using Qurihara;
using System.Collections;
using System.Xml;
using Qurihara.Anm;
using System.IO;
using OSC.NET;
using System.Threading;
using System.Net;
using Qurihara.QBasic;
using System.Text;
using System.Collections.Generic;
using Microsoft.Ink;
using System.Deployment.Application;
using System.Collections.Specialized;
using System.Web;


namespace KotodamaPlugin.BCAnalyzerPlugin
{
	/// <summary>
	/// Class1 の概要の説明です。
	/// </summary>
	public class BCAnalyzerPlugin :IPlugin
    {

        //public static string QRPrefix = "unryu.org:";


        private MenuItem menuConfig;
        //private MenuItem menuAutoSubmit;
        private MenuItem menuExport;

        private System.Windows.Forms.Timer timer;
        Qurihara.OSCCom.IComCore core;
        Qurihara.OSCCom.ComForAnalyzer com;

        public BCAnalyzerPlugin()
		{
            menuConfig = new MenuItem("Config");
            //menuConfig.Popup += new EventHandler(menuConfig_Popup);

            menuExport = new MenuItem("Export");
            menuExport.Click += new EventHandler(menuExport_Click);
            menuConfig.MenuItems.Add(menuExport);
		}

        void menuExport_Click(object sender, EventArgs e)
        {
            Stream vs = new FileStream("viewSnapList.xml",FileMode.Create);
            Stream ls = new FileStream("timeLog.csv", FileMode.Create);
            com.Export(vs,ls);
            MessageBox.Show("Exported.");
        }


        //void menuConfig_Popup(object sender, EventArgs e)
        //{
        //    menuAutoSubmit.Checked = com._sendInkImmediately;
        //}

        private Qurihara.QBasic.TextEntryForm tef;
        private void AskGroup(string defaultName)
        {

            tef = new TextEntryForm("30秒経つと自動的に決定します。", "分析したいグループ名を入力してください。");
            tef.OutputText = defaultName;
            tef.Closed += new EventHandler(tef_Closed);
            tef.Show();
        }

        private void tef_Closed(object sender, EventArgs e)
        {
            if (tef != null)
            {
                DialogResult dr = tef.DialogResult;
                if (dr == DialogResult.OK)
                {
                    // init

                    core = new Qurihara.OSCCom.SyncWebComCore();
                    //((Qurihara.OSCCom.WebComCore)core).GroupName = group;

                    //com = new Qurihara.OSCCom.ComForDisplay(Host.Kotodama, core);
                    com = new Qurihara.OSCCom.ComForAnalyzer(Host.Kotodama, core);
                    com.Initialize();
                    com.Name = "BCAnalyzer";

                    char[] trm = { '\n' };
                    ((Qurihara.OSCCom.WebComCore)core).GroupName = tef.OutputText.TrimEnd(trm);
                }
                tef.Dispose();
                tef = null;

                string str = ((Qurihara.OSCCom.WebComCore)core).GroupName;
                //Control c = (Control)Host.Kotodama.EnvironmentTable["Base.Form"];
                //c.Text = " [" + ((Qurihara.OSCCom.WebComCore)core).GroupName + "] " + c.Text + " ";

                //
                timer = new System.Windows.Forms.Timer();
                timer.Interval = 500;
                timer.Tick += new EventHandler(timer_Tick);
                timer.Enabled = true;
                timer.Start();

                Control c = (Control)Host.Kotodama.EnvironmentTable["Base.Form"];
                c.Text = " -" + "Analyzer" + "- " + c.Text;

                c.Text = " [" + ((Qurihara.OSCCom.WebComCore)core).GroupName + "] " + c.Text + " ";

            }
        }

		#region IPlugin Members
		
		IPluginHost myPluginHost = null;
		string myPluginName = "BCAnalyzer Plugin";
		string myPluginAuthor = "K.Qurihara";
		string myPluginDescription = "test plugin";
		string myPluginVersion = "1.0.0";

		public void Initialize()
		{

            Menu.MenuItemCollection mic = (Menu.MenuItemCollection)Host.Kotodama.EnvironmentTable["Base.MenuItems"];
            mic.Add(menuConfig);

            AskGroup("");

        }

		void PluginInterface.IPlugin.Dispose()
		{
            timer.Stop();
            timer.Enabled = false;
            timer.Dispose();
            com.Dispose();

            Menu.MenuItemCollection mic = (Menu.MenuItemCollection)Host.Kotodama.EnvironmentTable["Base.MenuItems"];
            mic.Remove(menuConfig);

		}

        #region misc
		public string Description
		{
			get
			{
				// TODO:  Add ctlMain.Description getter implementation
				return myPluginDescription;
			}
		}

		public string Author
		{
			get
			{
				// TODO:  Add ctlMain.Author getter implementation
				return myPluginAuthor;
			}
		}

		public IPluginHost Host
		{
			get
			{
				// TODO:  Add ctlMain.Host getter implementation
				return myPluginHost;
			}
			set
			{
				myPluginHost = value;
			}
		}

		
		public string Name
		{
			get
			{
				// TODO:  Add ctlMain.Name getter implementation
				return myPluginName;
			}
		}
		

		public string Version
		{
			get
			{
				// TODO:  Add ctlMain.Version getter implementation
				return myPluginVersion;
			}
        }
        #endregion

        #endregion

        void timer_Tick(object sender, EventArgs e)
        {
            Control c = (Control)Host.Kotodama.EnvironmentTable["Base.Form"];
            string t = c.Text;
            string [] ts = t.Split('@');
            string title = ts[0];
            if (core == null)
            {
                Delegate d = (MethodInvoker)delegate()
                {
                    c.Text = title + "@初期化中";
                };
                Host.Kotodama.Control.BeginInvoke(d);
            }else
            if (core.IsConnected)
            {
                Delegate d = (MethodInvoker)delegate()
                {
                    c.Text = title + "@接続中";
                };
                Host.Kotodama.Control.BeginInvoke(d);
            }
            else
            {
                Delegate d = (MethodInvoker)delegate()
                {
                    c.Text = title + "@非接続";
                };
                Host.Kotodama.Control.BeginInvoke(d);
            }


        }

    }
}